﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZFine.Code
{
    public class IDCardValidation
    {
        public IDCardValidation(string idNumber)
        {
            IsValidation = true;
            Message = "符合GB11643-1999标准";
            if (string.IsNullOrEmpty(idNumber))
            {
                IsValidation = false;
                Message = "身份证号不能为空";
            }
            else if (idNumber.Length == 18)
            {
                Init(idNumber);
            }
            else if (idNumber.Length == 15)//15位的转化为18位的
            {
                idNumber = idNumber.Trim().Insert(6, "19");
                string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
                string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
                char[] Ai = idNumber.ToCharArray();
                int sum = 0;
                for (int i = 0; i < 17; i++)
                {
                    sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());

                }
                int y = -1;
                Math.DivRem(sum, 11, out y);
                idNumber = idNumber + arrVarifyCode[y];
                Init(idNumber);
            }
            else
            {
                //长度
                IsValidation = false;
                Message = "身份证号码必须为15位或18位";
            }
        }

        #region 属性
        /// <summary>
        /// 身份证号是否可用
        /// </summary>
        public bool IsValidation
        {
            get;
            set;
        }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message
        {
            get;
            set;
        }
        /// <summary>
        /// 性别
        /// </summary>
        public string XB
        {
            get;
            set;
        }
        /// <summary>
        /// 出生年月
        /// </summary>
        public DateTime CSNY
        {
            get;
            set;
        }
        /// <summary>
        /// 退休日期
        /// </summary>
        public DateTime TXRQ
        {
            get;
            set;
        }
        /// <summary>
        /// 出生地
        /// </summary>
        public string CSD
        {
            get;
            set;
        }
        /// <summary>
        /// 年龄
        /// </summary>
        public int NL
        {
            get
            {
                DateTime beginDateTime = CSNY;
                DateTime endDateTime = DateTime.Now;
                int Months = endDateTime.Month - beginDateTime.Month + 12 * (endDateTime.Year - beginDateTime.Year);
                /*出生日期加总月数后，如果大于当前日期则减一个月*/
                int totalMonth = (beginDateTime.AddMonths(Months) > endDateTime) ? Months - 1 : Months;
                /*计算整年*/
                return totalMonth / 12;
            }
        }
        #endregion

        /// <summary>
        /// 18位身份证号码
        /// </summary>
        private void Init(string idNumber)
        {
            long n = 0;
            if (long.TryParse(idNumber.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(idNumber.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                IsValidation = false;
                Message = "身份证号码有非法字符，必须全部为数字";
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(idNumber.Remove(2)) == -1)
            {
                IsValidation = false;
            }
            string birth = idNumber.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                IsValidation = false;
                Message = "身份证号码出生日期部分有误";
            }
            else
            {
                CSNY = time;
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = idNumber.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());

            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != idNumber.Substring(17, 1).ToLower())
            {
                IsValidation = false;
                Message = "身份证号码校验位有误";
            }
            int PP = Convert.ToInt32(idNumber.Substring(16, 1)) % 2;//判断倒数第二位是奇数还是偶数
            if (PP == 0)//如果是偶数
            {
                XB = "女";//说明身份证号码的持有者是女性
                TXRQ = CSNY.AddYears(50);
            }
            else
            {
                XB = "男";//如果是奇数则身份证号码的持有者是男性
                TXRQ = CSNY.AddYears(60);
            }
            InitDz(idNumber);
        }

        private void InitDz(string idNumber)
        {
            CSD = "更新中……";
        }
    }
}

